package com.hp.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hp.context.Config;
import com.hp.dao.ResourcesTypeDao;
import com.hp.entity.ResourceTypeTreeVo;
import com.hp.entity.ResourcesType;
import com.hp.result.DataGridResultInfo;
import com.hp.result.ResultUtil;
import com.hp.result.SubmitResultInfo;
import com.hp.service.ResourcesTypeService;
import com.hp.utils.UUIDBuild;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Transactional
@Service
public class ResourcesTypeServiceImpl implements ResourcesTypeService {

    @Autowired
    private ResourcesTypeDao resourcesTypeDao;

    /**
     * @param page          第几页
     * @param rows          每页显示的数
     * @param resourcesType 查询(?)
     * @return
     * @throws Exception
     */
    @Transactional(readOnly = true)
    @Override
    public DataGridResultInfo pageresourcetype(int page, int rows, ResourcesType resourcesType) throws Exception {
       //SELECT * FROM tbl_type_psr WHERE parentid IS NOT NULL
        Page<Object> startPage = PageHelper.startPage(page, rows);
        resourcesTypeDao.findAllByParentIsNotNull(page, rows, resourcesType);
        DataGridResultInfo data = new DataGridResultInfo();
        data.setRows(startPage.getResult());
        data.setTotal((int) startPage.getTotal());
        return data;
    }

    /**
     * 添加资源类型
     * @param resourcesType
     * @return
     * @throws Exception
     */
    @Override
    public SubmitResultInfo savetype(ResourcesType resourcesType) throws Exception {

       //获取父类类型实体
        ResourcesType parentIsNull=  resourcesTypeDao.findByParentIsNull();

        //关联
        resourcesType.setParentid(parentIsNull.getId());
        resourcesType.setCreatetime(new Date());
        resourcesType.setId(UUIDBuild.getUUID());//sdfsd-sdfsdag-fhadfsad

        //保存
        resourcesTypeDao.savatype(resourcesType);

         //java new Object[]{}
        //C/C++ new Object[10]{}
        return ResultUtil.createSubmitResult(ResultUtil.createSuccess(Config.MESSAGE,661,new Object[]{"资源类型"}));
    }

    /**
     * 查询类型通过id
     * @param id
     * @return
     * @throws Exception
     */
    @Transactional(readOnly = true)
    @Override
    public ResourcesType findTypeById(String id) throws Exception {
        return resourcesTypeDao.findTypeById(id);
    }

    @Override
    public SubmitResultInfo updatetype(ResourcesType resourcesType) throws Exception {

        resourcesTypeDao.updateType(resourcesType);


        return ResultUtil.createSubmitResult(ResultUtil.createSuccess(Config.MESSAGE,666,new Object[]{"类型"}));

    }

    @Override
    public SubmitResultInfo deletetype(String typeid) throws Exception {
        resourcesTypeDao.deleteTypeById(typeid);
        return ResultUtil.createSubmitResult(ResultUtil.createSuccess(Config.MESSAGE,662,new Object[]{"类型"}));
    }

    @Override
    public ResourceTypeTreeVo getResourceTypeTreeVo() throws Exception {
        ResourcesType parent = resourcesTypeDao
                .getParent();
        ResourceTypeTreeVo resourceTreeVo = new ResourceTypeTreeVo(parent);
        return resourceTreeVo;
    }
}
